<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cookie;


class question extends Login
{
    /**
     * 显示资源列表
     * $arr 条件数组，默认为空
     * @return \think\Response
     */
    public function index($arr=array(),$page = 1)
    {
        $count = Db::name('question')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('question q, gqx_user u')->field('q.id,q.title,u.name,q.status')->where('u.id=q.uid')->where($arr)->order(['q.id'=>'asc'])->page($page,10)->select();
        // return(var_dump($data));
        return view('question/index',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'count'=>$count,
            'page'=>$page,
            'data'=>$data,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    public function search($page = 1,Request $request)
    {
        $post = $request->post();
        $count = Db::name('question')->where('title','like','%'.$post['like'].'%')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('question q, gqx_user u')->field('q.id,q.title,u.name,q.status')->where('u.id=q.uid')->where('title','like','%'.$post['like'].'%')->order(['q.id'=>'asc'])->page($page,10)->select();
        // return(var_dump($data));
        return view('question/index',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'count'=>$count,
            'page'=>$page,
            'data'=>$data,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        // return $id;
        // return $request->param();
        // 直接处理状态更改请求
        if(@$request->param()['source'] == 'status'){
            $data['status'] = $request->param()['status'];
            return Db::name('question')->where('id',$id)->update($data);
        }

    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        return Db::name('question')->where('id',$id)->delete();
    }
}
